<template lang="html">
  <div class="scan">
    <div class="bcid">

    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {}
  },
  mounted() {
    this.startScan()
  },
  methods: {
    startScan() {
      const vm = this
      let barcode = plus.barcode.create('bcid', [plus.barcode.QR], {
  			top: '0',
  			left: '0',
        right: '0',
        bottom: '0',
  			width: '100%',
  			height: '100%',
  			position: 'static'
  		});
  		// barcode.onmarked = this.onmarked;
  		let bc = plus.webview.currentWebview().append(barcode);
      // let scan = new plus.barcode.Barcode('bcid', 0) //eslint-disable-line
      plus.key.addEventListener('backbutton', function () { //eslint-disable-line
          // 事件处理
        barcode.close()
        barcode.cancel()
        vm.$router.go(-1)
        // vm.$router.replace({
        //   path: "/edit"
        // })
      }, false)
      barcode.onmarked = (type, result) => {
        switch (type) {
          case plus.barcode.QR:
            let _result = JSON.parse(result)
            barcode.close()
            barcode.cancel()
            // plus.webview.currentWebview().remove(barcode)
            vm.$router.replace({
              path: "/edit",
              query: {
                QRId: _result.FId,
                x: _result.X,
                y: _result.Y,
                FCode: _result.FCode
              }
            })
            break;
          case plus.barcode.EAN13:
            break;
          case plus.barcode.EAN8:
            break;
        }
      }
      barcode.start() //eslint-disable-line
    },
  }
}
</script>

<style lang="scss">
.scan {
  #bcid {
    height: 480px;
    width: 360px;
    background: #0F0;
  }
}
</style>
